# -*- coding: utf-8 -*-

'''
Created on 09-07-10

@author: peterd
'''




from web.service.LBCParser import LBCParser
from geo.models import NoSuchCity, NoSuchStreet, NoSuchLocation
from infra.logging import logger
from infra.lexical.CharMapper import *


import re

class OrgParser(LBCParser):
	'''
	classdocs
	'''

	def __init__(self, lbcategory, dataProcessor): 
		LBCParser.__init__(self, lbcategory, dataProcessor)

		self.inRestCand_ = False
		self.inRestType_ = False
		self.url_ = None
		self.inName_ = False
		self.name_ = None
		self.inStreetAddr_ = False
		self.matched_ = False
		self.numPages_ = 1

		
		log = logger.getLogger()
		log.debug("%s = %s" % (self, self.__dict__))
		
				
	def reset__(self):
		self.inRestCand_ = False
		self.inRestType_ = False
		self.url_ = None
		self.inName_ = False
		self.name_ = None
		self.inStreetAddr_ = False
		self.matched_ = False
		
		LBCParser.reset__(self)
		
		
	def preParse(self):
		self.numPages_ = 1


	def start_td(self, attrs):
		for attr, value in attrs:
			if attr == 'valign' and value == 'top':
				self.inRestCand_ = True
				break
			
	
	def end_td(self):
		if self.inRestCand_:
			self.reset__()
			self.inRestCand_ = False


	def start_span(self, attrs):
		if self.inRestCand_:
			for attr, value in attrs:
				if attr == 'class' and value == 'smaller2':
					if not self.name_:
						self.inRestType_ = True
					else:
						self.inStreetAddr_ = True
					break


	def end_span(self):
		if self.inRestType_:
			self.inRestType_ = False
		if self.inStreetAddr_:
			self.inStreetAddr_ = False
			


	def start_a(self, attrs):
		for attr, value in attrs:
			if attr == 'href':
				if re.search('\?pubfilter=[0-9]+(&SID=\w+)*&page=[0-9]+', value):
					self.numPages_ += 1
					break
				elif self.inRestCand_:
					self.url_ = self.dataProcessor_.getUrl(value)
					self.corId_ = getCorId(self.url_)
					break
	

	def start_b(self, attrs):
		if self.inRestCand_ and not self.name_:
			self.inName_ = True


	def end_b(self):
		if self.inName_:
			self.inName_ = False



	def do_handle_data(self, data):
		log = logger.getLogger()
		
		if self.inRestType_:
			self.matched_ = self.dataProcessor_.match(data)
			
		if self.inName_:
			self.name_ = data

		if self.matched_ and  self.alive() and self.inStreetAddr_:
			try:
				self.setStreet(data)				
				self.createOrganization(name = self.name_, url = self.url_)
			except NoSuchCity, e:
				log.error('NoSuchCity exception caught, reason: %s' % e)						
			except NoSuchStreet, e:
				log.error('NoSuchStreet exception caught, reason: %s' % e)
			except NoSuchLocation, e:
				log.error('NoSuchStreet exception caught, reason: %s' % e)
			except Exception, e:
				log.error('An exception caught, reason: %s' % e)
				
				
	def alive(self):
		return self.name_ and cp1250_to_ascii(self.name_).lower().find('zruseny') == -1
	
								
	def getId(self):
		return self.__class__.__name__
	
	
	def getNumPages(self):
		return self.numPages_
	
	
	
#
# utils
#

def getCorId(url):
	corId = None
	m = re.search('/([0-9]+)/', url)
	if m:
		corId = m.group(1)
	return corId	